<template>
  <el-dialog title="添加员工" :visible="showadd" @close="close">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">

      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>

      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in option" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>

      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="form.departmentName"
          placeholder="请选择"
          :options="options"
          :props="props"
          @focus="getdepartments"
        />
      </el-form-item>

      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="isyesadd">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import employees from '@/components/constant/employees'
import { getdepartments } from '@/api/departments'
import { addemployees } from '@/api/employees'
import { listToTree } from '@/utils'
export default {

  name: 'AddEmployee',
  props: {
    showadd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      option: employees.hireType,
      props: {
        label: 'name',
        value: 'name',
        expandTrigger: 'hover'
      },
      options: [],
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async getdepartments() {
      const res = await getdepartments()
      this.options = listToTree(res.depts)
    },
    close() {
      this.$emit('showaddoff')
      this.$refs.ruleForm.resetFields()
    },

    // 确定添加员工
    async isyesadd() {
      try {
        await this.$refs.ruleForm.validate()
      } catch {
        console.log(222)
        return
      }

      await addemployees({
        ...this.form, departmentName: this.form.departmentName.slice(-1)[0]
      })
      this.close()
      this.$message.success('操作成功')
    }
  }
}

</script>

<style>

</style>
